Mastering+Object-oriented+Python.compressed by Unknown

Mastering+Object-oriented+Python.compressed by Unknown

Author:Unknown
Language: eng
Format: mobi
Tags: www.it-ebooks.info
Published: 2015-09-10T19:50:08.116379+00:00


======================================================================

FAIL: test_should_returnStr (__main__.TestAceCard)

----------------------------------------------------------------------

Traceback (most recent call last):

File "p3_c15.py", line 80, in test_should_returnStr

self.assertEqual( "A♠", str(self.ace_spades) )

AssertionError: 'A♠' != '1♠'

- A♠

+ 1♠

[ 485 ]

Designing for Testability

======================================================================

FAIL: test_should_returnStr (__main__.TestFaceCard)

----------------------------------------------------------------------

Traceback (most recent call last):

File "p3_c15.py", line 91, in test_should_returnStr

self.assertEqual( "Q♥", str(self.queen_hearts) )

AssertionError: 'Q♥' != '12♥'

- Q♥

+ 12♥

----------------------------------------------------------------------

Ran 6 tests in 0.001s

FAILED (failures=2)

The TestLoader class created two tests from each TestCase class. This gives us a total of six tests. The test names are the method names that begin with test.

Clearly, we have a problem. Our tests provide an expected result that our class definitions don't meet. We've got more development work to do for the Card classes in order to pass this simple suite of unit tests. The fix should be clear and we'll leave it as an exercise for the reader.

Including edge and corner cases

When we move to testing the Deck class as a whole, we'll need to have some things confirmed: that it produces all of the required Cards class, and that it actually shuffles properly. We don't really need to test that it deals properly because we're depending on the list and list.pop() method; as these are first-class parts of Python, they don't require additional testing.

We'd like to test the Deck class construction and shuffling, independently of any specific Card class hierarchy. As noted previously, we can use a factory function to make the two Deck and Card definitions independent. Introducing a factory function introduces yet more testing. Not a bad thing, considering the bugs previously revealed in the Card class hierarchy.

Here's a test of the factory function:

class TestCardFactory( unittest.TestCase ):

def test_rank1_should_createAceCard( self ):

c = card( 1, '♣' )

self.assertIsInstance( c, AceCard )

def test_rank2_should_createCard( self ):

[ 486 ]

Chapter 15

c = card( 2, '♦' )

self.assertIsInstance( c, Card )

def test_rank10_should_createCard( self ):

c = card( 10, '♥' )

self.assertIsInstance( c, Card )

def test_rank10_should_createFaceCard( self ):

c = card( 11, '♠' )

self.assertIsInstance( c, Card )

def test_rank13_should_createFaceCard( self ):

c = card( 13, '♣' )

self.assertIsInstance( c, Card )

def test_otherRank_should_exception( self ):

with self.assertRaises( LogicError ):

c = card(14, '♦')

with self.assertRaises( LogicError ):

c = card(0, '♦')

We didn't test all 13 ranks, as 2 through 10 should all be identical. Instead, we followed this advice from Boris Beizer:

"Bugs lurk in corners and congregate at boundaries."

The test cases involve the edge values for each card range. Consequently, we have test cases for the values 1, 2, 10, 11, and 13, as well as illegal values of 0 and 14. We bracketed each range with the least value, the maximum value, one below the least value, and one above the maximum value.

When this is run, there will be problems reported by this test case too. One of the biggest problems will be an undefined exception, LogicError. This is simply a subclass of Exception that defines that the exception still isn't enough to get the test case to pass. The rest of the fix is left as an exercise for the reader.

Mocking dependencies for testing

In order to test Deck, we have two choices to handle the dependencies:

• Mocking: We can create a mock (or stand-in) class for the Card class and a mock card() factory function that produces the mock class.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Popular ebooks
Whisky: Malt Whiskies of Scotland (Collins Little Books) by dominic roskrow(55910)
What's Done in Darkness by Kayla Perrin(26522)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(19007)
The Fifty Shades Trilogy & Grey by E L James(18961)
Shot Through the Heart by Mercy Celeste(18880)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 10 by Isuna Hasekura and Jyuu Ayakura(16985)
Python GUI Applications using PyQt5 : The hands-on guide to build apps with Python by Verdugo Leire(16876)
Peren F. Statistics for Business and Economics...Essential Formulas 3ed 2025 by Unknown(16805)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 03 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16699)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 01 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16326)
The Subtle Art of Not Giving a F*ck by Mark Manson(14262)
The 3rd Cycle of the Betrayed Series Collection: Extremely Controversial Historical Thrillers (Betrayed Series Boxed set) by McCray Carolyn(14072)
Stepbrother Stories 2 - 21 Taboo Story Collection (Brother Sister Stepbrother Stepsister Taboo Pseudo Incest Family Virgin Creampie Pregnant Forced Pregnancy Breeding) by Roxi Harding(13421)
Scorched Earth by Nick Kyme(12716)
Drei Generationen auf dem Jakobsweg by Stein Pia(10923)
Suna by Ziefle Pia(10846)
Scythe by Neal Shusterman(10271)
International Relations from the Global South; Worlds of Difference; First Edition by Arlene B. Tickner & Karen Smith(9476)
Successful Proposal Strategies for Small Businesses: Using Knowledge Management ot Win Govenment, Private Sector, and International Contracts 3rd Edition by Robert Frey(9316)
This is Going to Hurt by Adam Kay(9099)